home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / jemtex2.zip / JIS2MF.C < prev    next >
C/C++ Source or Header  |  1991-04-14  |  38KB  |  997 lines

  1. /* -mt -f -A -K -G -O -w */
  2. /* Compile with Turbo-C 2.0 */
  3. /*
  4.   This program generates METAFONT code from a Bitmaps file JIS24
  5.  
  6.   Author: Francois Jalbert
  7.               '
  8.   Date: November 1990
  9.  
  10.   Version: 1.0
  11.  
  12.   Date: April 1991
  13.  
  14.   Version: 2.00
  15.  
  16.   Modifications: - Added four kanjis.
  17.                  - Fixed incorrect VGA resolution.
  18.                  - Command line parameter now supported.
  19.                  - Added automatic mode.
  20.                  - Added batch mode.
  21.                  - Updated and improved run-time messages.
  22.                  - Long triangles added by Mr. Masatoshi Watanabe. Fantastic!
  23.                  - Fixed and proportional parameters added.
  24.                  - Standard and dictionary parameters added.
  25.                  - JIS24 now accessed through low-level I/O channel for speed.
  26.  
  27.   Error Levels: 0 - Normal termination.
  28.                 1 - Error.
  29.                 2 - All fonts generated (batch).
  30. */
  31.  
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <ctype.h>
  35. #ifdef __TURBOC__
  36. #include <process.h>
  37. #endif
  38.  
  39. #define True  1
  40. #define False 0
  41. /* Number of Bitmaps in JIS24 */
  42. #define BitmapMax 7806
  43. /* Size of each square Bitmap */
  44. #define SizeMax  24
  45. #define SizeMax1 25
  46. /* DOS file name length */
  47. #define FileNameDOS  250 /* includes path */
  48. #define TotalNameDOS 254
  49. /* DOS Record Size */
  50. #define RecSize 72 /* SizeMax*SizeMax/8 */
  51. /* Parameter flag */
  52. #define Flag1 '/' /* DOS style */
  53. #define Flag2 '-' /* UNIX style */
  54. /* Parameter keywords */
  55. #define ParamMax 20
  56. char FixedX1[]="FIXEDWIDTH";
  57. char FixedX2[]="FIXEDX";
  58. char FixedX3[]="NOPROPORTIONALWIDTH";
  59. char FixedX4[]="NOPROPORTIONALX";
  60. char NoFixedX1[]="NOFIXEDWIDTH";
  61. char NoFixedX2[]="NOFIXEDX";
  62. char NoFixedX3[]="PROPORTIONALWIDTH";
  63. char NoFixedX4[]="PROPORTIONALX";
  64. char FixedY1[]="FIXEDHEIGHT";
  65. char FixedY2[]="FIXEDY";
  66. char FixedY3[]="NOPROPORTIONALHEIGHT";
  67. char FixedY4[]="NOPROPORTIONALY";
  68. char NoFixedY1[]="NOFIXEDHEIGHT";
  69. char NoFixedY2[]="NOFIXEDY";
  70. char NoFixedY3[]="PROPORTIONALHEIGHT";
  71. char NoFixedY4[]="PROPORTIONALY";
  72. char Standard1[]="STANDARD";
  73. char NoStandard1[]="DICTIONARY";
  74. char Batch1[]="BATCH";
  75. /* Answer maximum length */
  76. #define AnswerMax 254
  77.  
  78. typedef char FileNameType[FileNameDOS+1];
  79. typedef char TotalNameType[TotalNameDOS+1];
  80. typedef char ParamType[ParamMax+1];
  81. typedef char AnswerType[AnswerMax+1];
  82. /* Buffer for the Bitmap Data */
  83. struct ColumnType {
  84.   unsigned char Data1,Data2,Data3;
  85. };
  86. typedef struct ColumnType BufferType[SizeMax]; /* start at 0 because of fread */
  87. /* The Bitmap array is defined larger to simplify the forthcoming code */
  88. typedef int BitmapType[SizeMax1+1][SizeMax1+1];
  89. struct BitmapsType {
  90.   BitmapType Bitmap;
  91.   int XMin,XMax,YMin,YMax;
  92. };
  93. /* Run time parameters */
  94. struct RunTimeType {
  95.   FileNameType FileName;
  96.   /* Batch mode */
  97.   int Batch;
  98.   /* Automatic mode for JemTeX fonts only */
  99.   int Automatic;
  100.   /* Fixed or proportional fonts */
  101.   int FixedX,FixedY;
  102.   /* Standard or dictionary fonts */
  103.   int Standard;
  104. };
  105.  
  106. void Delete(char *String, int Length)
  107. /* Delete the first Length characters of String */
  108. {
  109.   int Index;
  110.  
  111.   Index=0;
  112.   do String[Index]=String[Index+Length];
  113.   while (String[++Index]!='\0');
  114. }
  115.  
  116. /*------------------------------- GetParameters -----------------------------*/
  117.  
  118. void SimpleQuery(char Title[], char ChoiceA[], char ChoiceB[], int *Answer)
  119. {
  120.   char JChar[2];
  121.   int Valid;
  122.  
  123.   do {
  124.     Valid=True;
  125.     printf("%s:\n",Title);
  126.     printf("   a)  %s\n",ChoiceA);
  127.     printf("   b)  %s\n",ChoiceB);
  128.     printf("Your choice? ");
  129.     if (ferror(stdout)) exit(1);
  130.     if (gets(JChar)==NULL) exit(1);
  131.     if (strlen(JChar)>1) exit(1);
  132.     JChar[0]=toupper(JChar[0]);
  133.     if (JChar[0]=='A') *Answer=True;
  134.     else
  135.       if (JChar[0]=='B') *Answer=False;
  136.       else { 
  137.         Valid=False; 
  138.         if (putchar('\7')==EOF) exit(1); 
  139.     }
  140.   } while (!Valid);
  141.   printf("\n");
  142.   if (ferror(stdout)) exit(1);
  143. }
  144.  
  145. void GetMode(struct RunTimeType *RunTime)
  146. /* Determines if the desired font is a JemTeX font */
  147. {
  148.   RunTime->Automatic=False;
  149.   if (toupper(RunTime->FileName[0])=='K')
  150.   if (toupper(RunTime->FileName[1])=='A')
  151.   if (toupper(RunTime->FileName[2])=='N')
  152.   if (toupper(RunTime->FileName[3])=='J')
  153.   if (toupper(RunTime->FileName[4])=='I')
  154.   if(('A'<=toupper(RunTime->FileName[5]))&&(toupper(RunTime->FileName[5])<='H'))
  155.   if(('A'<=toupper(RunTime->FileName[6]))&&(toupper(RunTime->FileName[6])<='H'))
  156.   if (strlen(RunTime->FileName)==7)
  157.   if (toupper(RunTime->FileName[5])<='G') RunTime->Automatic=True;
  158.   else
  159.   if (toupper(RunTime->FileName[6])<='E') RunTime->Automatic=True;
  160. }
  161.  
  162. void EchoParameters(struct RunTimeType *RunTime)
  163. /* Echoes the current parameters */
  164. {
  165.   printf("Font=%s",RunTime->FileName);
  166.   if (RunTime->FixedX) printf("  Fixed Width");
  167.   else printf("  Prop. Width");
  168.   if (RunTime->FixedY) printf("  Fixed Height");
  169.   else printf("  Prop. Height");
  170.   if (RunTime->Standard) printf("  Standard");
  171.   else printf("  Dictionary");
  172.   if (RunTime->Automatic) printf("  Automatic");
  173.   else printf("  Manual");
  174.   if (RunTime->Batch) printf("  Batch");
  175.   printf(".\n");
  176.   if (ferror(stdout)) exit(1);
  177. }
  178.  
  179. void Manual(struct RunTimeType *RunTime)
  180. /* Get parameters from user */
  181. {
  182.   printf("METAFONT file name? ");
  183.   if (ferror(stdout)) exit(1);
  184.   if (gets(RunTime->FileName)==NULL) exit(1);
  185.   if (strlen(RunTime->FileName)>FileNameDOS) {
  186.     /* File name too long */
  187.     printf("\7File name too long: %s...",RunTime->FileName);
  188.     exit(1);
  189.   }
  190.   printf("\n");
  191.   if (ferror(stdout)) exit(1);
  192.   SimpleQuery("Fixed or proportional font width","Fixed","Proportional",
  193.               &RunTime->FixedX);
  194.   SimpleQuery("Fixed or proportional font height","Fixed","Proportional",
  195.               &RunTime->FixedY);
  196.   SimpleQuery("Standard or dictionary font","Standard","Dictionary",
  197.               &RunTime->Standard);
  198.   /* Batch mode intrinsically isn't manual */
  199.   RunTime->Batch=False;
  200. }
  201.  
  202. void FindBefore(FileNameType FileName)
  203. /* No check for before kanjiaa */
  204. {
  205.   if (FileName[6]=='a') {
  206.     FileName[6]='h';
  207.     FileName[5]--;
  208.   }
  209.   else
  210.     FileName[6]--;
  211. }
  212.  
  213. void FindAfter(FileNameType FileName)
  214. /* No check for above kanjihe */
  215. {
  216.   if (FileName[6]=='h') {
  217.     FileName[6]='a';
  218.     FileName[5]++;
  219.   }
  220.   else
  221.     FileName[6]++;
  222. }
  223.  
  224. void ScanMF(FileNameType FileName)
  225. /* Scans backwards for the last JemTeX font generated */
  226. /* Looks first for a .TFM and then for an .MF */
  227. /* If no more fonts to generate, stops with error level 2 */
  228. {
  229.   FILE *TestFile;
  230.   TotalNameType TotalName;
  231.   int Found;
  232.  
  233.   strcpy(FileName,"kanjihf");
  234.   do {
  235.     FindBefore(FileName);
  236.     strcpy(TotalName,FileName);
  237.     strcat(TotalName,".tfm");
  238.     TestFile=fopen(TotalName,"r");
  239.     Found=(TestFile!=NULL);
  240.     if (!Found) {
  241.       strcpy(TotalName,FileName);
  242.       strcat(TotalName,".mf");
  243.       TestFile=fopen(TotalName,"r");
  244.       Found=(TestFile!=NULL);
  245.     }
  246.   } while (!Found && strcmp(FileName,"kanjiaa"));
  247.   if (Found) {
  248.     if (fclose(TestFile)==EOF) exit(1);
  249.     if (strcmp(FileName,"kanjihe")) FindAfter(FileName);
  250.     else {
  251.       printf("\7All JemTeX fonts generated!\n");
  252.       exit(2);
  253.     }
  254.   }
  255. }
  256.  
  257. void Automate(struct RunTimeType *RunTime, int argc, char *argv[])
  258. /* Get parameters from command line */
  259. /* Finds the next font to be generated if in batch mode */
  260. {
  261.   int ParamIndex,ParamLength,Index;
  262.   ParamType Param;
  263.  
  264.   /* Defaults */
  265.   strcpy(RunTime->FileName,"kanjiaa");
  266.   RunTime->FixedX=False;
  267.   RunTime->FixedY=False;
  268.   RunTime->Standard=True;
  269.   RunTime->Batch=False;
  270.   /* Scan command line parameters */
  271.   /* 0th is program's name, last is NULL */
  272.   for (ParamIndex=1 ; ParamIndex<argc ; ParamIndex++) {
  273.     ParamLength=strlen(argv[ParamIndex]);
  274.     if (ParamLength>ParamMax) {
  275.       /* Keyword too long */
  276.       printf("\7Invalid command line parameter: %s...",argv[ParamIndex]);
  277.       exit(1);
  278.     }
  279.     strcpy(Param,argv[ParamIndex]);
  280.     if ((Param[0]==Flag1) || (Param[0]==Flag2)) {
  281.       /* Not a font name */
  282.       /* Delete 1 char at t